package com.example.database_manage.administractor;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;



import com.example.database_manage.R;
import com.example.database_manage.database.CommonDatabase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Common_Fragment extends Fragment {
    String listview_state="";
    SQLiteDatabase db;
    private Toolbar toolbar;
    private ListView listView;

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.common_toolbar_menu,menu);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.guanliyuan_backup_add:
                startActivity(new Intent(getActivity(),add_admin.class));


                break;

        }
        return true;
    }



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_admin,null);
        return  view;
    }

    @Override
    public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //Toolbar设置
        toolbar = view.findViewById(R.id.toolbar_fragment_common);
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

        setHasOptionsMenu(true);


        //查看管理员信息
        Button button_look_admin = view.findViewById(R.id.look_admin);

        db = new CommonDatabase().getSqliteObject(getActivity(), "test_db");
        listView = view.findViewById(R.id.f_c_listview);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(listview_state.equals("change_course_set"))
                {
                    HashMap<String,Object > map_item = (HashMap<String,Object >)listView.getItemAtPosition(position);



                }

                else if(listview_state.equals("admin"))
                {
                    HashMap<String,Object > map_item = (HashMap<String,Object >)listView.getItemAtPosition(position);

                }

            }
        });

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                switch (v.getId()) {
                    /******查看课程设置情况******///

                    case R.id.look_admin:
                        listview_state = "admin";
                        Cursor cursor = db.query("administractor",null,null,null,null,null, null);
                        ArrayList<Map<String, String>> arrayList_look_admin = new ArrayList<Map<String, String>>();
                        while(cursor.moveToNext())
                        {
                            Map<String, String> map = new HashMap<String, String>();
                            map.put("account",cursor.getString(cursor.getColumnIndex("account")));
                            map.put("password",cursor.getString(cursor.getColumnIndex("password")));
                            arrayList_look_admin.add(map);

                        }
                        SimpleAdapter simpleAdapter_look_admin = new SimpleAdapter(getActivity(), arrayList_look_admin, R.layout.list_item_account,
                                new String[]{"account","password"}, new int[]{R.id.account_t, R.id.account_tv});
                        listView.setAdapter(simpleAdapter_look_admin);
                        break;
                    default:
                        break;

                }
            }


        };

         button_look_admin.setOnClickListener(listener);
    }

}
